package com.x19i.common.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

import static com.x19i.common.Constants.*;

/**
 * @author Su.Xiaosong
 * @date 2024-08-16
 * @since 1.0
 */
public class DateUtils {


    public static final DateTimeFormatter DTF_HH_MI_SS = DateTimeFormatter.ofPattern(LOCAL_TIME_FORMAT);
    public static final DateTimeFormatter DTF_YYYY_MM_DD = DateTimeFormatter.ofPattern(LOCAL_DATE_FORMAT);
    public static final DateTimeFormatter DTF_YYYY_MM_DD__HH_MI_SS = DateTimeFormatter.ofPattern(LOCAL_DATE_TIME_FORMAT);


    public static LocalDateTime string2LocalDateTime(String str) {
        if (StrUtil.isEmpty(str)) return null;
        try {
            return LocalDateTime.parse(str, DTF_YYYY_MM_DD__HH_MI_SS);
        } catch (Exception ex) {
            return null;
        }
    }

    public static LocalDate string2LocalDate(String str) {
        if (StrUtil.isEmpty(str)) return null;
        try {
            return LocalDate.parse(str, DTF_YYYY_MM_DD);
        } catch (Exception ex) {
            return null;
        }
    }

    public static LocalTime string2LocalTime(String str) {
        if (StrUtil.isEmpty(str)) return null;
        try {
            return LocalTime.parse(str, DTF_HH_MI_SS);
        } catch (Exception ex) {
            return null;
        }
    }


    public static String asString(LocalTime ld) {
        if (null == ld) return null;
        return ld.format(DTF_HH_MI_SS);
    }


    public static String asString(LocalDate ld) {
        if (null == ld) return null;
        return ld.format(DTF_YYYY_MM_DD);
    }

    public static String asString(LocalDateTime ldt) {
        if (null == ldt) return null;
        return ldt.format(DTF_YYYY_MM_DD__HH_MI_SS);
    }


}
